﻿namespace TriangleGames.TrainResEditor
{
    using TriangleGames.TrainRes;
    using System;
    using System.Collections.Generic;
    using UnityEditor;
    using UnityEngine;

    internal class FsmInspector
    {
        private static bool chooseWatermark;
        private static List<FsmVariable> fsmVariables = new List<FsmVariable>();
        private static bool isInitialized;
        private static Vector2 scrollViewPosition;

        private static void BuildFsmVariableList(Fsm fsm)
        {
            fsmVariables = FsmVariable.GetFsmVariableList(fsm.Variables, fsm.Owner);
            List<FsmVariable> list = new List<FsmVariable>();
            foreach (FsmVariable variable in fsmVariables)
            {
                if (!variable.ShowInInspector)
                {
                    list.Add(variable);
                }
            }
            foreach (FsmVariable variable2 in list)
            {
                fsmVariables.Remove(variable2);
            }
            fsmVariables.Sort();
        }

        public static void Init()
        {
            if (FsmEditor.SelectedFsm != null)
            {
                BuildFsmVariableList(FsmEditor.SelectedFsm);
            }
        }

        public static void OnGUI()
        {
            GUI.changed = false;
            Fsm selectedFsm = FsmEditor.SelectedFsm;
            if (selectedFsm == null)
            {
                GUILayout.FlexibleSpace();
            }
            else
            {
                if (!isInitialized)
                {
                    isInitialized = true;
                    Init();
                }
                scrollViewPosition = GUILayout.BeginScrollView(scrollViewPosition, new GUILayoutOption[0]);
                selectedFsm.Name = EditorGUILayout.TextField(selectedFsm.Name, new GUILayoutOption[0]);
                selectedFsm.Description = FsmEditorGUILayout.TextAreaWithHint(selectedFsm.Description, "描述...", new GUILayoutOption[] { GUILayout.MinHeight(80f) });
                EditorGUILayout.BeginHorizontal(new GUILayoutOption[0]);
                selectedFsm.DocUrl = FsmEditorGUILayout.TextFieldWithHint(selectedFsm.DocUrl, "文档链接...", new GUILayoutOption[0]);
                bool enabled = GUI.enabled;
                if (string.IsNullOrEmpty(selectedFsm.DocUrl))
                {
                    GUI.enabled = false;
                }
                if (FsmEditorGUILayout.HelpButton("在线帮助"))
                {
                    Application.OpenURL(selectedFsm.DocUrl);
                }
                EditorGUILayout.EndHorizontal();
                GUI.enabled = enabled;
                selectedFsm.RestartOnEnable = GUILayout.Toggle(selectedFsm.RestartOnEnable, new GUIContent("禁用时重置", "在激活/禁用的时候，状态机是否重置或保持当前状态."), new GUILayoutOption[0]);
                selectedFsm.ShowStateLabel = GUILayout.Toggle(selectedFsm.ShowStateLabel, new GUIContent("显示状态标签", "在运行窗口显示激活状态标签.\n注意: 需要添加TrainResGUI"), new GUILayoutOption[0]);
                selectedFsm.EnableDebugFlow = GUILayout.Toggle(selectedFsm.EnableDebugFlow, new GUIContent("激活调试", "运行时缓存变量和其他状态信息. 注意: 禁用此选项可以提高性能."), new GUILayoutOption[0]);
                FsmEditorGUILayout.LightDivider(new GUILayoutOption[0]);
                GUILayout.Label(new GUIContent("控件", "在观察器中显示变量和事件."), EditorStyles.boldLabel, new GUILayoutOption[0]);
                if ((selectedFsm.ExposedEvents.Count + fsmVariables.Count) == 0)
                {
                    FsmEditorGUILayout.DisabledLabel("[空]");
                }
                else
                {
                    foreach (FsmVariable variable in fsmVariables)
                    {
                        if (variable.ShowInInspector)
                        {
                            variable.DoValueGUI(new GUIContent(variable.Name, variable.Name + (!string.IsNullOrEmpty(variable.Tooltip) ? (":\n" + variable.Tooltip) : "")), false);
                        }
                    }
                    foreach (FsmEvent event2 in selectedFsm.ExposedEvents)
                    {
                        if (GUILayout.Button(event2.Name, new GUILayoutOption[0]))
                        {
                            selectedFsm.Event(event2);
                        }
                    }
                    if (GUI.changed)
                    {
                        FsmEditor.RepaintAll();
                    }
                }
                if (FsmEditorSettings.ShowHints)
                {
                    GUILayout.Box("在观察器中公布事件和变量.\n运行时，你可以这些变量进行测试和调整.", FsmEditorStyles.HintBox, new GUILayoutOption[0]);
                }
                FsmEditorGUILayout.LightDivider(new GUILayoutOption[0]);
                GUILayout.Label(new GUIContent("网络同步", "设置网络同步的变量."), EditorStyles.boldLabel, new GUILayoutOption[0]);
                int num = 0;
                foreach (FsmBool @bool in selectedFsm.Variables.BoolVariables)
                {
                    if (@bool.NetworkSync)
                    {
                        GUILayout.Label(@bool.Name, new GUILayoutOption[0]);
                        num++;
                    }
                }
                foreach (FsmFloat num2 in selectedFsm.Variables.FloatVariables)
                {
                    if (num2.NetworkSync)
                    {
                        GUILayout.Label(num2.Name, new GUILayoutOption[0]);
                        num++;
                    }
                }
                foreach (FsmInt num3 in selectedFsm.Variables.IntVariables)
                {
                    if (num3.NetworkSync)
                    {
                        GUILayout.Label(num3.Name, new GUILayoutOption[0]);
                        num++;
                    }
                }
                foreach (FsmQuaternion quaternion in selectedFsm.Variables.QuaternionVariables)
                {
                    if (quaternion.NetworkSync)
                    {
                        GUILayout.Label(quaternion.Name, new GUILayoutOption[0]);
                        num++;
                    }
                }
                foreach (FsmVector3 vector in selectedFsm.Variables.Vector3Variables)
                {
                    if (vector.NetworkSync)
                    {
                        GUILayout.Label(vector.Name, new GUILayoutOption[0]);
                        num++;
                    }
                }
                if (num == 0)
                {
                    FsmEditorGUILayout.DisabledLabel("[空]");
                }
                if (FsmEditorSettings.ShowHints)
                {
                    GUILayout.Box("设置网络同步的变量. 注意: 对象需要一个Network View组件.", FsmEditorStyles.HintBox, new GUILayoutOption[0]);
                }
                FsmEditorGUILayout.LightDivider(new GUILayoutOption[0]);
                GUILayout.EndScrollView();
                EventType type = Event.current.type;
                if (GUI.changed)
                {
                    FsmEditor.SetFsmDirty(false, false);
                }
            }
        }

        public static void Update()
        {
            if (chooseWatermark)
            {
                chooseWatermark = false;
                EditorCommands.ChooseWatermark();
            }
        }
    }
}

